package merlin.dao;

import java.util.*;
import java.sql.*;

public class WebsiteReportRequestsDAO implements java.io.Serializable{

	Connection con;

	public WebsiteReportRequestsDAO(Connection connection){
		con = connection;
	}

	public int[] addWebsiteReportRequests(int websiteEnquiryId, int arrReportTitleIds[])throws SQLException{

		PreparedStatement pstmt = con.prepareStatement("INSERT INTO WEBSITE_REPORT_REQUESTS(WEBSITE_ENQUIRIES_ID,REPORT_TITLES_ID,REQUEST_DATE)VALUES(?,?,CURRENT_DATE)");
		for(int i=0;i<arrReportTitleIds.length;i++){
			pstmt.setInt(1,websiteEnquiryId);
			pstmt.setInt(2,arrReportTitleIds[i]);
			pstmt.addBatch();
		}
		int[] updateCols = pstmt.executeBatch();
		pstmt.close();
		return updateCols;
	}

/*
	public WebsiteReportRequestsBean selectWebsiteReportRequestsByPK(WebsiteReportRequestsBean bean)throws SQLException{

		Connection con = getConnection();
		PreparedStatement pstmt = con.prepareStatement("SELECT WEBSITE_REPORT_REQUESTS_ID,WEBSITE_ENQUIRIES_ID,REPORT_TITLES_ID,REQUEST_DATE FROM WEBSITE_REPORT_REQUESTS WHERE WEBSITE_REPORT_REQUESTS_ID = ? ");
		pstmt.setInt(1,bean.getWebsiteReportRequestsId());
		ResultSet rs = pstmt.executeQuery();
		if(rs.next()){
			WebsiteReportRequestsBean bn = new WebsiteReportRequestsBean();
			bn.setWebsiteReportRequestsId(rs.getInt("WEBSITE_REPORT_REQUESTS_ID"));
			bn.setWebsiteEnquiriesId(rs.getInt("WEBSITE_ENQUIRIES_ID"));
			bn.setReportTitlesId(rs.getInt("REPORT_TITLES_ID"));
			bn.setRequestDate(rs.getDate("REQUEST_DATE"));
			returnConnection(con);
			return bn;
		}else{
			returnConnection(con);
			return null;
		}
	}

	public DbResultSet selectWebsiteReportRequestsRecords(String whereClause,String orderByClause)throws SQLException{

		ArrayList records = new ArrayList();
		Connection con = getConnection();
		String sql ="SELECT WEBSITE_REPORT_REQUESTS_ID,WEBSITE_ENQUIRIES_ID,REPORT_TITLES_ID,REQUEST_DATE FROM WEBSITE_REPORT_REQUESTS";
		if(whereClause !=null && !whereClause.equals(""))sql += " WHERE " + whereClause;
		if(orderByClause !=null && !orderByClause.equals(""))sql += " ORDER BY " + orderByClause;
		Statement stmt =  con.createStatement();
		ResultSet rs =  stmt.executeQuery(sql);
		while(rs.next()){
			WebsiteReportRequestsBean bn = new WebsiteReportRequestsBean(rs.getInt("WEBSITE_REPORT_REQUESTS_ID"),rs.getInt("WEBSITE_ENQUIRIES_ID"),rs.getInt("REPORT_TITLES_ID"),rs.getDate("REQUEST_DATE"));
			records.add(bn);
		}
		rs.close();
		stmt.close();
		returnConnection(con);
		if(!records.isEmpty())return new DbResultSet(records,records.size()); else return new DbResultSet();
	}



	public int updateWebsiteReportRequests(WebsiteReportRequestsBean bean)throws SQLException{

		Connection con = getConnection();
		con.setAutoCommit(false);
		PreparedStatement pstmt = con.prepareStatement("UPDATE WEBSITE_REPORT_REQUESTS SET WEBSITE_ENQUIRIES_ID = ? ,REPORT_TITLES_ID = ? ,REQUEST_DATE = ?  WHERE WEBSITE_REPORT_REQUESTS_ID = ?");
		pstmt.setInt(1,bean.getWebsiteEnquiriesId());
		pstmt.setInt(2,bean.getReportTitlesId());
		pstmt.setDate(3,bean.getRequestDate());
		pstmt.setInt(4,bean.getWebsiteReportRequestsId());
		int updateCols = pstmt.executeUpdate();
		con.commit();
		pstmt.close();
		returnConnection(con);
		return updateCols;
	}

	public int deleteWebsiteReportRequests(WebsiteReportRequestsBean bean)throws SQLException{

		Connection con = getConnection();
		con.setAutoCommit(false);
		PreparedStatement pstmt = con.prepareStatement("DELETE FROM WEBSITE_REPORT_REQUESTS WHERE WEBSITE_REPORT_REQUESTS_ID = ?");
		pstmt.setInt(1,bean.getWebsiteReportRequestsId());
		int updateCols = pstmt.executeUpdate();
		con.commit();
		pstmt.close();
		returnConnection(con);
		return updateCols;
	}
	*/
}